package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.BlogMapper;
import com.itheima.dao.TagMapper;
import com.itheima.pojo.Blog;
import com.itheima.pojo.Picture;
import com.itheima.pojo.Tag;
import com.itheima.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private BlogMapper blogMapper;
    /**
     * 2020年8月4日09:39:07
     * 增加标签
     * @param tag 标签实体类
     */
    @Override
    public void add(Tag tag) {
        tagMapper.insertSelective(tag);
    }

    /**
     * 根据uid删除标签
     * @param uid
     */
    @Override
    public void deleteByUid(String uid) {
        Tag tag = new Tag();
        tag.setUid(uid);
        tag.setStatus(0);
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    /**
     * 编辑标签
     * @param tag
     */
    @Override
    public void edit(Tag tag) {
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    /**
     * 根据关键词分页查询
     * @param searchMap
     * @return
     */
    @Override
    public Page<Tag> findPage(Map searchMap) {
        Integer currentPage=null;
        if(searchMap.get("currentPage")!=null){
             currentPage = (Integer) searchMap.get("currentPage");
        }else {
            currentPage =1;
        }
        Integer pageSize=null;
        if( searchMap.get("pageSize")!=null){
             pageSize = (Integer) searchMap.get("pageSize");
        }else {
            pageSize=10;
        }

        Example example=createExample(searchMap);
        PageHelper.startPage(currentPage,pageSize);
        return (Page<Tag>) tagMapper.selectByExample(example);
    }

    /**
     * 置顶标签
     * @param tag
     */
    @Override
    public void stick(Tag tag) {
        Integer maxSort = tagMapper.getMaxSort();
        tag.setSort(maxSort+1);
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    /**
     * 根据引用排序
     */
    @Override
    @Transactional
    public void tagSortByCite() {
        List<Map<String, Object>> tagCount = blogMapper.findTagCount();
        tagMapper.initSort();
        for (Map<String, Object> map : tagCount) {
            Tag tag = new Tag();
            tag.setUid((String) map.get("tag_uid"));
            long count = (long) map.get("count");
            tag.setSort(Long.valueOf(count).intValue());
            tagMapper.updateByPrimaryKeySelective(tag);
        }

    }

    /**
     * 根据点击排序
     */
    @Override
    @Transactional
    public void tagSortByClickCount() {
        tagMapper.tagSortByClickCount();
    }

    // 获取最热标签
    @Override
    public Page<Tag> getHotTag(Integer pageSize) {
        PageHelper.startPage(1, pageSize);
        Example example = new Example(Tag.class);
        example.setOrderByClause("click_count desc, sort desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 1);
        List<Tag> tagList = tagMapper.selectByExample(example);
        Page<Tag> pageList = (Page<Tag>) tagList;
        return pageList;
    }


    //根据TagUid获取文章
    @Override
    public Page<Blog> getArticleByTagUid(String uid, Integer currentPage, Integer pageSize) {

        if (currentPage == null) {
            currentPage = 1;
        }
        if (pageSize == null) {
            pageSize =5;
        }
        PageHelper.startPage(currentPage, pageSize);

        Example example = new Example(Blog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tagUid", uid);
        Page<Blog>blogPage= (Page<Blog>) blogMapper.selectByExample(example);
        return blogPage;
    }

    //获取标签的信息
    @Override
    public Page<Tag> getTagList(Integer currentPage, Integer pageSize) {
        if (currentPage == null) {
            currentPage = 1;
        }
        if (pageSize == null) {
            pageSize =5;
        }
        PageHelper.startPage(currentPage, pageSize);
        Page<Tag> tagPage= (Page<Tag>) tagMapper.selectAll();
        return tagPage;
    }


    /**
     * 创建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map searchMap) {
        Example example = new Example(Tag.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("update_time desc");
        if(searchMap.get("uid")!=null&&!"".equals(searchMap.get("uid"))){
            criteria.andEqualTo("uid",searchMap.get("uid"));
        }
        if(searchMap.get("content")!=null&&!"".equals(searchMap.get("content"))){
            criteria.andLike("content","%"+searchMap.get("content")+"%");
        }

        return example;
    }
}
